<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 9:32
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\UnitModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UnitController extends Controller
{
    /**
     * 计量单位列表
     * @return \Illuminate\Http\Response
     */
    public function lists()
    {
        $unitModel = new UnitModel();
        $this->data = $unitModel->lists();
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取计量单位参数失败',
            'id.integer' => '计量单位参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $unitModel = new UnitModel();
            $this->data = $unitModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 230101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'shortname' => 'required',
        ];
        $message = [
            'name.required' => '请输入计量单位名称',
            'shortname.required' => '请输入计量单位简称',
        ];
        $param = $request->only(['name','shortname']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $unitModel = new UnitModel();
            $insertId = $unitModel->insert($param);
            if (!is_int($insertId)){
                throw new ApiException('保存失败', 230202);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 230201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'shortname' => 'required',
        ];
        $message = [
            'id.required' => '获取计量单位参数失败',
            'id.integer' => '计量单位参数类型错误',
            'name.required' => '请输入计量单位名称',
            'shortname.required' => '请输入计量单位简称',
        ];
        $param = $request->only(['name','shortname','id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $unitModel = new UnitModel();
            $id = $param['id'];unset($param['id']);
            $unitModel->update(['id'=>$id],$param);
        } else {
            throw new ApiException($validator->errors()->first(), 230301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取计量单位参数失败',
            'id.integer' => '计量单位参数类型错误',
            'status.required' => '未获取到状态',
            'status.integer' => '计量单位状态参数类型错误',
            'status.in' => '计量单位状态参数值不正确',
        ];
        $param = $request->only('id','status');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $unitModel = new UnitModel();
            $unitModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 230401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}